﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NcfDB.Model
{
    class FieldProperty
    {
        public string Name { get; set; }
        public FieldType Type { get; set; }
        public int Length = -1;
        public bool isPrimaryKey = false;
        public bool acceptNull = true;

        public static FieldProperty readFromStream(ref FileStream fs,ref StreamReader sr)
        {
            //StreamReader sr = new StreamReader(fs);
            FieldProperty fieldproperty = new FieldProperty();
            fieldproperty.Name = sr.ReadLine();
            if (fieldproperty.Name == null || fieldproperty.Name == "~") return null;
            fieldproperty.Type = (FieldType) Enum.Parse(typeof(FieldType), sr.ReadLine(), true);
            fieldproperty.Length = int.Parse(sr.ReadLine());
            fieldproperty.isPrimaryKey = Convert.ToBoolean(sr.ReadLine());
            fieldproperty.acceptNull = Convert.ToBoolean(sr.ReadLine());
            return fieldproperty;
        }

        public void save(ref FileStream fs)
        {
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(this.Name);
            sw.WriteLine(this.Type.ToString());
            sw.WriteLine(this.Length);
            sw.WriteLine(this.isPrimaryKey);
            sw.WriteLine(this.acceptNull);
            //Utility.writeBytes(Encoding.Default.GetBytes(this.Name), ref fs);
            //Utility.writeBytes(Encoding.Default.GetBytes(this.Type.ToString()), ref fs);
            //Utility.writeBytes(BitConverter.GetBytes(this.Length), ref fs);
            //Utility.writeBytes(BitConverter.GetBytes(this.isPrimaryKey), ref fs);
            //Utility.writeBytes(BitConverter.GetBytes(this.acceptNull), ref fs);
            sw.Flush();
        }
    }

}
